// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.editors;

import org.dengues.core.process.EConnectionType;
import org.dengues.designer.ui.editors.actions.GEFCopyAction;
import org.dengues.designer.ui.editors.actions.GEFCutAction;
import org.dengues.designer.ui.editors.actions.GEFDeleteAction;
import org.dengues.designer.ui.editors.actions.GEFPasteAction;
import org.dengues.designer.ui.i18n.Messages;
import org.dengues.designer.ui.process.actions.ConnectionCreateAction;
import org.eclipse.gef.ContextMenuProvider;
import org.eclipse.gef.EditPartViewer;
import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.actions.GEFActionConstants;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.actions.ActionFactory;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 1 2006-09-29 17:06:40Z qiang.zhang $
 * 
 */
public class GEFEditorContextMenuProvider extends ContextMenuProvider {

    private final ActionRegistry actionRegistry;

    private final IWorkbenchPart part;

    private static final String GROUP_CONNECTIONS = "ConnectionsGroup"; //$NON-NLS-1$

    /**
     * Qiang.Zhang.Adolf@gmail.com GEFEditorContextMenuProvider constructor comment.
     * 
     * @param part
     * @param viewer
     * @param registry
     */
    public GEFEditorContextMenuProvider(IWorkbenchPart part, EditPartViewer viewer, ActionRegistry registry) {
        super(viewer);
        if (registry == null) {
            throw new IllegalArgumentException();
        }
        actionRegistry = registry;
        this.part = part;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.gef.ContextMenuProvider#buildContextMenu(org.eclipse.jface.action.IMenuManager)
     */
    @Override
    public void buildContextMenu(IMenuManager menu) {
        if (part != null) {
            IAction action;
            action = getAction(ConnectionCreateAction.ID + EConnectionType.MAIN.toString());
            if (action != null) {
                ConnectionCreateAction connAction = ((ConnectionCreateAction) action);
                connAction.update();
                if (connAction.isEnabled()) {
                    menu.add(new Separator(GROUP_CONNECTIONS));
                    MenuManager subMenu = new MenuManager(Messages.getString("GEFEditorContextMenuProvider.row")); //$NON-NLS-1$
                    menu.appendToGroup(GROUP_CONNECTIONS, subMenu);
                    subMenu.add(action);
                }
            }
            menu.add(new Separator(GEFActionConstants.GROUP_UNDO));
            menu.add(new Separator(GEFActionConstants.GROUP_COPY));
            menu.add(new Separator(GEFActionConstants.GROUP_EDIT));

            menu.appendToGroup(GEFActionConstants.GROUP_UNDO, getAction(ActionFactory.UNDO.getId()));
            menu.appendToGroup(GEFActionConstants.GROUP_UNDO, getAction(ActionFactory.REDO.getId()));

            action = getAction(ActionFactory.COPY.getId());
            ((GEFCopyAction) action).update();
            menu.appendToGroup(GEFActionConstants.GROUP_COPY, action);

            action = getAction(ActionFactory.CUT.getId());
            if (action != null) {
                ((GEFCutAction) action).update();
                menu.appendToGroup(GEFActionConstants.GROUP_COPY, action);
            }

            action = getAction(ActionFactory.PASTE.getId());
            ((GEFPasteAction) action).update();
            menu.appendToGroup(GEFActionConstants.GROUP_COPY, action);

            action = getAction(ActionFactory.DELETE.getId());
            ((GEFDeleteAction) action).update();
            menu.appendToGroup(GEFActionConstants.GROUP_EDIT, action);
            menu.appendToGroup(GEFActionConstants.GROUP_EDIT, getAction(ActionFactory.SELECT_ALL.getId()));
        }

    }

    private IAction getAction(final String actionId) {
        return actionRegistry.getAction(actionId);
    }

}
